// pages/person/purchaseLevel/purchaseLevel.js

import WxParse from '../../../wxParse/wxParse.js'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showModalStatus: false,
    cardBg: '',
    levelid: 0,
    level: 0,
    title: '',
    price: 0,
    user: {},
    package: '',
    radioid: '',
    levelmsg: '',
    buttonshow:true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this
    let cardBg = ""
    that.setData({
      levelid: options.levelid,
      level: options.level,
      title: options.title,
      price: options.price,
      cardBg: cardBg
    })
    console.log('传递信息', options)
    that.getStorage()
    let user = wx.getStorageSync('userinfo')
    console.log('user',user)
    wx.request({
      url: app.globalData.api + "shenqingyy",
      data: {
        uid: user.id,
        level: options.level
      },
      success: function (res) {
        console.log('礼包', res)
        if (res.data.code == 1) {
          that.setData({
            buttonshow: false,
          })
        }
      },
    })
  },
  radioChange: function(e) {
    let that = this
    console.log(e)

    console.log('radio：', e.detail.value)
    let packagea = that.data.package[e.detail.value]
    let html = '暂无数据'
    if (packagea.content!=null){
      html = packagea.content.replace(/\<img/gi, '<img style="width:100%;height:auto;display: block" ')
    }
    console.log('package', packagea)
    console.log('html',html)
    this.setData({
      radioid: e.detail.value,
      yuliu: html,
      titleyl: packagea.title,

      guigeid: packagea.id, //礼包的id
      showModalStatus:true
    })
  },
  showBuyModal() {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,

      timingFunction: "ease",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(), // export 方法每次调用后会清掉之前的动画操作。
      showModalStatus: true
    })
    setTimeout(() => {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export() // export 方法每次调用后会清掉之前的动画操作。
      })
      console.log(this)
    }, 200)
  },

  hideBuyModal() {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "ease",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: false
      })
      console.log(this)
    }.bind(this), 200)
  },



  chooseAddress1: function () {
    wx.navigateTo({
      url: '/pages/address/address',
    })
  },
  /**
   * 获取用户信息从内存中
   */
  getStorage: function() {
    let that = this
    wx.getStorage({
      key: 'userinfo',
      success: function(res) {
        console.log('内存用户信息', res)
        that.setData({
          user: res.data
        })
        console.log(that.data.user)
        //会员礼包
        wx.request({
          url: app.globalData.api + "getleveldetail",
          data: {
            uid: res.data.id,
            id: that.data.level
          },
          success: function(res) {
            console.log('礼包', res)
           
            that.setData({
            
              package: res.data.list.programmelist, 
              levelmsg: res.data.list
            })
            var temp = WxParse.wxParse('goodsDesc', 'html', res.data.list.programmelist.content, that, 0);
            
          },
        })
        
      },
    })
  },

  /**
   * 购买卡片
   */
  purchase: function() {
    let that=this
    let cardPrice = Number(this.data.price)
    let cardTitle = Number(this.data.title)
    let addressid = that.data.addressid
    let guigeid = that.data.guigeid
    console.log(addressid, guigeid)
    if (!addressid){
      wx.showToast({
        title: '请选择地址',
        icon: 'none'
      })
    }
   else if (!that.data.radioid) {
      wx.showToast({
        title: '请选择一个商品',
        icon: 'none'
      })
    } else {
      wx.showToast({
        title: '请求中.....',
        icon: 'loading',
        duration: 1000
      })

      wx.request({
        url: 'https://bzl.isoft.mobi/index.php/Home/Paylevel/pay',
        // url: 'https://bzl.isoft.mobi/index.php/Home/Paylevelceshi/pay',

        data: {
          uid: Number(that.data.user.id),
          // id: that.data.radioid,
          id:that.data.guigeid,
          addressid: that.data.addressid,
          price: Number(that.data.price)
        },
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        method: "POST",
        success: (res)=> {
          console.log('申请',res)
          if (res.data.msg == '库存不足') {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          } else{
            console.log('支付', res)
            let json = res.data
            console.log(json.appId, json.timeStamp, json.nonceStr, json.package, json.paySign)
            wx.requestPayment({
              appId: json.appId,
              timeStamp: json.timeStamp,
              nonceStr: json.nonceStr,
              package: json.package,
              signType: 'MD5',
              paySign: json.paySign,
              success: (res) => {
                console.log(res)
                wx.redirectTo({
                  url: '/pages/buynow/buysuccess/buysuccess?show=' + 3 + '&price=' + cardPrice + '&id=' + cardTitle,
                })
              },
              fail: (res) => {
                console.log(res)
                wx.redirectTo({
                  url: '/pages/buynow/buysuccess/buysuccess?show=' + 2 + '&price=' + cardPrice + '&id=' + cardTitle,
                })
              }
            })
          }
        }
      })
    }
  },



//   else if(that.data.level== 5){
//   if (res.statusCode == 200) {
//     wx.showToast({
//       title: '申请成功',
//       icon: 'none'
//     })
//     wx.redirectTo({
//       url: '/pages/person/personIndex',
//     })
//   } else {
//     wx.showToast({
//       title: '申请失败，请稍后重试',
//       icon: 'none'
//     })
//   }
// }
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

      let that = this
      wx.getStorage({
        key: 'userinfo',
        success: function (res) {
          console.log(res.data.id)
          let uid = res.data.id
          that.setData({
            uid: uid

          })
          wx.request({
            url: app.globalData.api + 'getAddress', //我的收货地址列表
            data: {
              uid: that.data.uid,
              page: 1,
              limit: 1
            },
            success: function (res) {
              console.log(res)
              var result = res.data.list[0]
              console.log(result);
              let address = result.address_provinces_title + result.address_city_title + result.address_areas_title + result.address_title
              that.setData({
                username: result.username,
                phone: result.phone,
                address: address,
                addressid: result.id,
              })
              console.log(that.data.username, that.data.phone, that.data.address, that.data.addressid)

              // if (result == user.id) {

              // } else {
              //   that.setData({
              //     allAddress: result
              //   });
              // }

            }
          });
        },
      })
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})